﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
public partial class quytrinh2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["PreviousPage"] = Request.UrlReferrer;
        }
        connection ketnoi = new connection();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        double tongtien = 0;
        if (Request["sanpham"] != null)
        {
            string str = Request["sanpham"];
            string soluong = Request["soluong"];
            string sql = "SELECT *,"+ soluong + " as soluong,"+soluong+"*giasanpham as tongtien FROM SANPHAM where masanpham='" + str + "'";
            repeater.DataSource = ketnoi.Ds_table(ds,sql, "SANPHAM");
            repeater.DataBind();
            dt = ketnoi.return_table(ds,sql, "SANPHAM") ;
        }
        if (Request["shop"] != null)
        {
            string shop = Request["shop"];
            List<string> list = new List<string>();
            list = (List<string>)Session["Cart"];
            foreach (var item in list)
            {
                string[] t = item.Split('-');
                string sanpham = t[0];
                string soluong = t[1];
                string sql = "select *," + t[1] + " as soluong,"+t[1]+" * giasanpham as tongtien from Shop s,Sanpham sp where sp.Masanpham ='" + t[0] + "' and s.Mashop = sp.Mashop and s.Mashop='" + shop + "'";

                repeater.DataSource = ketnoi.Ds_table(ds,sql, "SANPHAM_SHOP");
                repeater.DataBind();
                dt = ds.Tables["SANPHAM_SHOP"];
                
            }
        }
        foreach (DataRow dr in dt.Rows)
        {
            tongtien += Convert.ToDouble(dr["tongtien"]);
        }
        quytrinh2_tongtien.Text = tongtien.ToString();
        ketnoi.Close();
    }
    public void quytrinh3(object sender, EventArgs e)
    {
        Session["tongtien"] = quytrinh2_tongtien.Text;
        if (Request["sanpham"] != null)
        {
            string soluong = Request["soluong"];
            string sanpham = Request["sanpham"];
            Response.Redirect("quytrinh3.aspx?soluong=" + soluong + "&sanpham=" + sanpham);
        }
        if (Request["shop"] != null)
        {
            string shop = Request["shop"];
            Response.Redirect("quytrinh3.aspx?shop=" + shop);
        }
        
    }
    public void back(object sender, EventArgs e)
    {
        if (ViewState["PreviousPage"] != null)	//Check if the ViewState 
        //contains Previous page URL
        {
            Response.Redirect(ViewState["PreviousPage"].ToString());//Redirect to 
            //Previous page by retrieving the PreviousPage Url from ViewState.
        }
    }
    
}